
package cjos.archivos;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author Jos
 */
public class DescargaArchivosUrl {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws MalformedURLException, IOException {
        System.out.println("Inicia Descarga");
        
        String url_ = "http://www.tecnun.es/asignaturas/Informat1/AyudaInf/aprendainf/Java/Java2.pdf";

        URL url = new URL(url_);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        String nombreArchivo = url.getFile().substring(url.getFile().lastIndexOf("/") + 1, url.getFile().length());
        InputStream in = connection.getInputStream();

        FileOutputStream out = new FileOutputStream(new File("C:\\" + nombreArchivo));
        
        int b = 0;
        while (b != -1) {
            b = in.read();
            if (b != -1) {
                out.write(b);
            }
        }       

        out.close();
        in.close();
        
        System.out.println("Termina Descarga");
    }
}
